#include"cmd_mysql.h"
int init_Usertask(mysql_info_t *mysql_info,taskStack_t *taskStack,char*UserName)
{
    bzero(mysql_info->sql_commd,4096);
    bzero(mysql_info->sql_commd,4096);
    //printf("6 init\n");
    sprintf(mysql_info->sql_commd,"select id from file_info where user = '%s' and pre_id = '0';",UserName);
    read_mysql(mysql_info);
    enStack(taskStack,mysql_info->sql_result);
    strcpy(taskStack->pFront->data,mysql_info->sql_result);
    /// @brief test
    //printf("%s\n",taskStack->pFront->data);
}
int read_mysql(mysql_info_t *mysql_info)
{
    bzero(mysql_info->sql_result,4096);
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *ret = mysql_real_connect(mysql,mysql_info->host,mysql_info->user,
                                    mysql_info->passwad,mysql_info->databases,0,NULL,0);
    if(ret == NULL)
    {
        fprintf(stderr,"error:%d\n",mysql_errno(mysql));
        printf("cmd 23 error\n");
        return -1;//报错
        
    }
    /// @brief sql_commd test
    //printf("sql = %s\n", mysql_info->sql_commd);
    int qret =  mysql_query(mysql,mysql_info->sql_commd);
    if(qret !=0)
    {
        fprintf(stderr,"error:%d\n",mysql_errno(mysql));
        printf("cmd 29 error\n");
        return -1;
        
    }
    //读
    MYSQL_RES *res = mysql_store_result(mysql);
    MYSQL_ROW row;
    bzero(mysql_info->sql_result,4096);
    int num = mysql_num_rows(res);
    //printf("row num = %d\n",num);
    char buf2[4096]={0};
    if(row == 1)
    {
        strcpy(mysql_info->sql_result,row[0]);
        goto end;
    }
    else
    {
        while((row = mysql_fetch_row(res))!=NULL)
        {
            for(int i=0;i<mysql_num_fields(res);i++)
            {
                strcat(mysql_info->sql_result,row[i]);
                strcat(mysql_info->sql_result," ");
            }
        }
    }
    end:
    return 0;
}
//cd
int cd_mysql(mysql_info_t *mysql_info,char*UserName,taskStack_t *taskStack,char*client_path)
{
    //先判断路径是否合法
    char path[1024] = {0};
    //printf("id = %s,path = %s\n",taskStack->pFront->data,client_path);
    char cutPath[4096];
    strcpy(cutPath,client_path);
    //printf("client path = %s\n",client_path);
    char *sockPath = strtok(cutPath,"/");
    //printf("sockpath = %s\n",sockPath);
    int num = 0;
    while(1)
    {
        while(sockPath == NULL)
        {
            //printf("this is run\n");
            goto end;
        }
        if(strcmp(sockPath,".")!=0 && strcmp(sockPath,"..")!=0)
        {
            char id[10]={0};
            bzero(id,1024);
            strcpy(id,taskStack->pFront->data);
            bzero(mysql_info->sql_commd,4096);
            //这里之前写错了
            bzero(mysql_info->sql_result,4096);
            //printf("sockpath = %s\n",sockPath);
            //printf("pre_id = %s\n",taskStack->pFront->data);
            sprintf(mysql_info->sql_commd,"select id from file_info where filename = '%s' and user = '%s' and pre_id = '%s';",sockPath,UserName,id);            
            int ret = read_mysql(mysql_info);
            //num ++;
            //printf("num = %d\n",num);
            //printf("read_mysql_ret = %d\n",ret);
            if(ret != 0)
            {
                ERROR_CHECK(ret,-1,"83 fail\n");
                return -1;
            }
            enStack(taskStack,mysql_info->sql_result);
            printf("mysql_pFront id = %s\n",taskStack->pFront->data);
        }
        if(strcmp(sockPath,"..")==0)
        {
            deStack(taskStack);
        }
        sockPath = strtok(NULL,"/");
    }
end:

/// @brief cd test 
    //printf("path = %s\n",path);
    //printf("path2 = %s\n",mysql_info->sql_result);
    return 0;
}

//pwd
int pwd_mysql(mysql_info_t *mysql_info,taskStack_t *taskStack,char*UserName)
{
    bzero(mysql_info->sql_commd,4096);
    bzero(mysql_info->sql_commd,4096);
    //根据pTail 拿到 user
    sprintf(mysql_info->sql_commd,"select user from file_info where id = '%s';",taskStack->pTail->data);
    char path[1024] = {0};
    read_mysql(mysql_info);
    strcpy(path,mysql_info->sql_result); //zs
    //进行第二次查找
    bzero(mysql_info->sql_commd,4096);
    bzero(mysql_info->sql_commd,4096);
    sprintf(mysql_info->sql_commd,"select filepath from file_info where user = '%s' and id = '%s';",UserName,taskStack->pFront->data);
    /// @brief pwd test
    //sprintf(mysql_info->sql_commd,"select filepath from file_info where user = '%s' and id = '3';",UserName,taskStack->pFront->data);
    //展示最顶层的pid
    read_mysql(mysql_info);
    strcat(path,mysql_info->sql_result); //zs/dir1
    //printf("188 id = %s\n",taskStack->pFront->data);
    printf("path = %s\n",path);
}

//ls
int ls_mysql(mysql_info_t *mysql_info,taskStack_t *taskStack,char*UserName)
{
    bzero(mysql_info->sql_commd,4096);
    bzero(mysql_info->sql_result,4096);
    sprintf(mysql_info->sql_commd,"select filename from file_info where pre_id = '%s';",taskStack->pFront->data);
    read_mysql(mysql_info);
    //printf("ls :186:buf = %s\n",mysql_info->sql_result);
    printf("ls = %s\n",mysql_info->sql_result);
    return 0;
}